package com.gofunbuy.app.member;

import java.util.ArrayList;
import java.util.HashMap;

import com.gofunbuy.R;
import com.gofunbuy.TabMemberGroup;
import com.gofunbuy.TabOthersGroup;
import com.gofunbuy.app.others.TabOthersActivity;
import com.gofunbuy.http.HttpClientImpl;
import com.gofunbuy.util.GoFunBuyUtils;
import com.gofunbuy.util.GoFunBuyUtils.GoFunBuyInterface;
import com.gofunbuy.util.GoFunBuyUtils.GoFunBuyInterface.ProtocalStatics;
import com.gofunbuy.util.GoFunBuyUtils.PayHistoryXML;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SubMemberPayHistoryActivity extends Activity implements
		OnClickListener, GoFunBuyInterface {
	private TextView tvBack, tvTitle;
	private ListView mPayHistoryListView;
	// private ArrayList<PayHistoryItem> mPayHistoryList = new
	// ArrayList<PayHistoryItem>();
	private ArrayList<PayHistoryXML> mPayHistoryList = new ArrayList<PayHistoryXML>();
	private PayHistoryAdapter mAdapter;
	private Context mContext;
	private int backToWhich = -1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.member_pay_history_layout);
		mContext = this;
		backToWhich = getIntent().getIntExtra(BACK_TO_WHO, -1);
		initLayout();
	}

	private void initLayout() {
		tvTitle = (TextView) findViewById(R.id.topbar_title);
		tvTitle.setText(R.string.title_pay_record);
		tvBack = (TextView) findViewById(R.id.topbar_back);
		tvBack.setOnClickListener(this);
		mPayHistoryListView = (ListView) findViewById(R.id.pay_history_listview);
		mAdapter = new PayHistoryAdapter(mContext);
		mPayHistoryListView.setAdapter(mAdapter);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				if (!GoFunBuyUtils.isNetworkConnected(mContext)) {
					mHandler.sendEmptyMessage(MSG_NETWORD_ERROR);
					return;
				}
				HashMap<String, String> params = new HashMap<String, String>();
				try {
					params.put(ProtocalStatics.SHOPCODE, "goinside");
					params.put(ProtocalStatics.USERID, GoFunBuyUtils.mUserID);
					// mPayHistoryList.clear();
					mPayHistoryList = HttpClientImpl.getInstance(mContext)
							.getReceiptList(mHandler, params);
					if (mPayHistoryList == null) {
						mHandler.sendMessage(mHandler.obtainMessage(
								MSG_GET_MESSAGE_STATE, -1, 0));
					} else {
						mHandler.sendMessage(mHandler.obtainMessage(
								MSG_GET_MESSAGE_STATE, 1, 0));
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}).start();

	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.topbar_back:
			if (backToWhich == 5)
				startOthersActivity();
			else
				startActivity(SubMemberDocumentActivity.class);
			break;
		}
	}

	private void startOthersActivity() {
		// 要跳转的Activity
		// Log.v("名字", a.getSimpleName() + " " + a.getName());
		Intent intent = new Intent(this, TabOthersActivity.class);
		// 把Activity转换成一个Window，然后转换成View
		Window w = TabOthersGroup.group.getLocalActivityManager()
				.startActivity(TabOthersActivity.class.getSimpleName(), intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		TabOthersGroup.group.setContentView(view);
	}

	private void startActivity(Class<?> a) {
		// 要跳转的Activity
		// Log.v("名字", a.getSimpleName() + " " + a.getName());
		Intent intent = new Intent(SubMemberPayHistoryActivity.this, a);
		// 把Activity转换成一个Window，然后转换成View
		Window w = TabMemberGroup.group.getLocalActivityManager()
				.startActivity(a.getSimpleName(), intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		TabMemberGroup.group.setContentView(view);
	}

	private void addTestData() {
		// mPayHistoryList.clear();
		// PayHistoryItem item = new PayHistoryItem();
		// item.time = 1378217571990L;
		// item.cost = "95";
		// item.goods = "紅茶";
		// item.integrations = "3,4";
		// item.donations = "$1.3";
		// mPayHistoryList.add(item);
		//
		// item = new PayHistoryItem();
		// item.time = 1378217571990L;
		// item.cost = "85";
		// item.goods = "奶茶";
		// item.integrations = "2,4";
		// item.donations = "$2.3";
		// mPayHistoryList.add(item);

	}

	class PayHistoryItem {
		public long time;// 消費時間
		public String goods;// 商品種類
		public String cost;// 消費金額
		public String integrations;// 積分
		public String donations;// 捐贈
	}

	public class PayHistoryAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		// private TextView title, time, mainContent;
		// private Button deleteButton, GPSOperationButton;
		// private ImageView envelopView;
		class ItemView {
			public TextView tvTime;
			public TextView tvGoods;
			public TextView tvCost;
			public TextView tvIntegration;
			public TextView tvDonation;
		}

		public PayHistoryAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
			// mInflater = (LayoutInflater)
			// getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		public int getCount() {
			// TODO Auto-generated method stub
			return mPayHistoryList.size();
		}

		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			// Log.v("myDebug", "getItem is called!pose is " + arg0);
			return mPayHistoryList.get(arg0);
		}

		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			// focusPosition = arg0;

			return arg0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ItemView holder;
			if (convertView == null) {
				convertView = mInflater
						.inflate(R.layout.pay_history_item, null);
				holder = new ItemView();

				holder.tvTime = (TextView) convertView
						.findViewById(R.id.pay_time);
				holder.tvGoods = (TextView) convertView
						.findViewById(R.id.pay_history_goods);
				holder.tvCost = (TextView) convertView
						.findViewById(R.id.pay_history_cost);
				holder.tvIntegration = (TextView) convertView
						.findViewById(R.id.pay_history_integration);
				holder.tvDonation = (TextView) convertView
						.findViewById(R.id.pay_history_donation);
				convertView.setTag(holder);
			} else {
				holder = (ItemView) convertView.getTag();
			}
			PayHistoryXML appUnit = mPayHistoryList.get(position);
			holder.tvTime.setText(appUnit.time);
			holder.tvGoods.setText(appUnit.goods);
			holder.tvCost.setText(appUnit.cost);
			holder.tvIntegration.setText(appUnit.integration);
			holder.tvDonation.setText(appUnit.donation);
			return convertView;
		}

		public void setSelectItem(int selectItem) {
			// Log.v("tag", ">>>>>>>>selectItem:" + selectItem);
			this.selectItem = selectItem;
		}

		private int selectItem = -1;

	}

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case MSG_NETWORD_ERROR:
				Toast.makeText(mContext, R.string.network_error,
						Toast.LENGTH_SHORT).show();
				break;
			case MSG_GET_MESSAGE_STATE:
				if (msg.arg1 == -1)
					Toast.makeText(mContext, R.string.donate_message_fail,
							Toast.LENGTH_SHORT).show();
				else if (msg.arg1 == 1) {
					mAdapter.notifyDataSetChanged();
				}
				break;
			}
		}

	};
}
